<?php
/**
 * dragon.hu add @2015.12.11  player_append
 */

defined('BASEPATH') or die('forbidden');

class Append_Controller Extends Controller {

    private $rolename;
    private $inc;
	private $joblist;	//职务列表
	private $professionList;	//职业列表
	private $newguideList;  //新手指引
	private $newguideType;  //指引类型
    private $m;	//数据库连接
    private $server_info;
	
    function _init() {
        check_level(50, segment(0), segment(1));
        set_time_limit(0);
        $this->inc = $this->config['inc'];
        $this->professionList = $this->inc['type_proffessionid'];
        //得到选择的区号
        $areanum = $_SESSION['area_selected_id'];
        if ($areanum == '') 
        {
            echo "<script>alert('" . translate('C_SELECT_AREA_TEXT') . "');</script>";
            return;
        }
        //query server mapping
        $selectSql = "SELECT * FROM server_mapping WHERE dbtype='1' AND areanum={$areanum} ";
        //$selectSql = "SELECT * FROM server_mapping WHERE dbtype='1' ";
        $this->db->query($selectSql);
        $this->server_info=$this->db->get_array('default');

        // db数据库
        $this->m = new MongoClient("mongodb://{$this->server_info['username']}:{$this->server_info['userpwd']}@{$this->server_info['dbip']}");
//        $this->newguideList = array(
//        		'1' => '初次战斗', '2' => '开启青铜宝箱', '3' => '背包2指引', '4' => '指引到一个副本及战斗指引', '5' => '背包3指引', '6' => '领取奖励', '7' => '第二副本战斗中指引', '8' => '接任务及第三副本战斗指引', '9' => '主界面橙色戒指提示', '10' => '学习技能',
//        		'11' => '第四副本战斗中指引','12' => '抽宠物', '13' => '宠物出战', '23' => '副本指引及第五副本战斗中指引', '29' => '熔炼指引', '' => '', '30' => '用熔炼值购买装备', '31' => '一键穿装', '32' => '一键装备升级', '33' => '竞技场指引',
//        		'34' => '秘境指引','35' => '夺宝指引', '36' => '点金指引', '37' => '矿洞', '38' => '矿洞2', '39' => '公会指引', '41' => '宠物冒险团', '43' => '荣誉巅峰', '90' => '人物属性培养', '99' => '宠物培养', '100' => '制指引结束', '102' => '金币副本',
//        		 '103' => '生存副本', '104' => '精英副本', '105' => '骑士团参列指引', '106' => '骑士团设为骑乘指引', '107' => '骑士团召唤指引'
//        );
//
//        $this->newguideType = array(
//        		'0' => '未触发', '1' => '进行中', '2' => '完成了'
//        );
//
//        // db数据库
//		$this->dbConnt = getTmpDbConnect(PLATFORM_ALL, 'all', $areanum, DBTYPE_GAMESERVER);
    }

    function index() {
        $this->view->display("dbplayer/append/index");
    }

    function flexigrid() {

        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();
        $query=$_POST['query'];
        $query_type=$_POST['qtype'];
        if($query_type=='_id'){
            $filter=array($query_type=>(float)$query);
        }else{
            $filter=array($query_type=>$query);
        }
        $collection=$this->m->{$this->server_info['dbname']}->PlayerRole;
        if($query!=''){
            $document=$collection->find($filter);
        }else{
            $document=$collection->find();
        }
        $data=array();
        $total = count($document);

        foreach ($document as $key=>$value){
            //$value['logofftime'] = get_datetime($value['logofftime']);
            //$value['loginday'] = get_datetime($value['loginday']);
            foreach ($value['actions'] as $k=>$v){
                $value['actions'][$k]=$v['value'].'-'.$v['time'];
            }
            $data[$key]=array('id' => $value['_id'], 'cell' => $value);
        }


//        $sql = "SELECT COUNT(*) AS num FROM player_append where {$where} ";
//        //echo " sql2:$sql ";
//        $one = $this->dbConnt->one($sql);
//        $total = $one['num'];
//        $orderby = $sortname ? "ORDER BY {$sortname} {$sortorder} " : "";
//
//        $sql = "SELECT {$colsinfo} FROM player_append where {$where}   {$orderby} LIMIT {$offset},{$rp}";
//        $this->dbConnt->query($sql);
//
//        while ($n = $this->dbConnt->get_array('default')) {
//        	//var_dump($n['newguidestate']);
//        	$tmpnum = Append_Controller::getTarget($n['newguidestate']);
//        	$n['newguidestate'] = $this->newguideList[$tmpnum]."(".$this->newguideType[$n['newguidestate'][$tmpnum - 1]].")";
//            $rows[] = array('id' => $n['id'], 'cell' => $n);
//        }
        //exit();
        //var_dump($rows);
        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $data
        );
        echo json_encode($result);
    }
    
    static function getTarget($str)
    {
    	$tmp = strlen($str);
    	for ($i = $tmp-1; $i >= 0; $i--)
    	{
    		if ($str[$i] > 0)
    		{
    			//var_dump($str[$i]);
    			return ($i + 1);
    			//var_dump($i);
    			//break;
    		}
    		//var_dump($str[$i]);
    	}
    }

}

?>
